$(function () {
    /*获取验证码*/
    $('.getYZMBtn').click(function () {
        var zhi = $(this).text();
        var regex = /^[1][3,4,5,7,8][0-9]{9}$/;
        var myPhone = $('#userPhone').val();
        if ($('#userPhone').val().length !== 0 && regex.test(myPhone) == true) {
            if (zhi == "获取验证码") {
                var sum = 60;
                var count = 1;
                
                ym.ajax({
                    url: "/base/getRandom",
                    param: {
                        phone: $('#userPhone').val()
                    },
                    success: function (data) {
                        if (data.res == 0) {
                            console.log($(".getYZMBtn").text())
                            $(".getYZMBtn").text('获取验证码');
                            $("#my-alert").modal('open');
                            $('#my-alert .am-modal-bd').html(data.resMsg);
							setTimeout(function(){
								$("#my-alert").modal('close');
							},500)
                        }else{
                            var i = setInterval(function () {
                                if (count > 59) {
                                    $(".getYZMBtn").text('获取验证码');
                                    clearInterval(i);
                                } else {
                                    $(".getYZMBtn").text('剩余' + parseInt(sum - count) + '秒');
                                }
                                count++;
                            }, 1000);
                        }
                    }
                });
            }
        } else {
            /*$('#userPhone').val()*/
            $('#my-alert .am-modal-bd').html('手机号码格式不正确');
            $("#my-alert").modal('open');
            setTimeout(function(){
            	 $("#my-alert").modal('close');
            },500)
        }
    })

    /*提交表单注册用户*/
    $('#subBtn').click(function () {
        if($('#userPWD').val().length!==0){
            $('#subBtn').attr('disabled','disabled');
            var dataJson = {
                userPhone: $('#userPhone').val(),
                random: $('#YZM').val(),
                password: $('#userPWD').val(),
            }
            ym.ajax({
                url: "/base/register",
                param: dataJson,
                success: function (data) {
                    $('#subBtn').removeAttr('disabled','disabled');
                    if (data.res !== 0) {
                        $('#subBtn').attr("data-am-modal", "{target: '#my-alert'}");
                        $('.am-modal-bd').html("注册成功，3秒后跳转到首页,<a href='../html/index'>点击跳转</a>");
                        window.setTimeout(function () {
                            window.location.href = '../html/index.html';
                        }, 3000)
                    } else {
                        $('#my-alert').modal('open');
                        $('#my-alert .am-modal-bd').html(data.resMsg);
                        window.setTimeout(function(){
                            $('#my-alert').modal('close');
                        },500)
                    }
                }
            });
        }else{
            $('#my-alert').modal('open');
            $('#my-alert .am-modal-bd').html('密码不能为空');
            window.setTimeout(function(){
                $('#my-alert').modal('close');
            },500)
        }
    })
})